package Datos;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.CallableStatement;
import java.util.*;

public class Contraparte {
	Sesion oSesion;
	
	public Contraparte() {};
	
	public int Add(Entidades.Contraparte item) throws Exception{
		try{
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.ContraparteAdd(?,?,?)}";
		CallableStatement cs =oCnn.prepareCall(storedProcedure);
		poneDatos(item,cs);
		boolean pipi = cs.execute();
		int numero = 0;
		if (pipi) numero = 1;
		return numero;
		
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
			   return -1;
		}
	}
	public void Update(Entidades.Contraparte item) throws Exception{
		try{
			Connection oCnn = Datos.General.CrearConexion();
			String storedProcedure = "{call cpd.ContraparteUpdate(?,?,?)}";
			CallableStatement cs =oCnn.prepareCall(storedProcedure);
			poneDatos(item,cs);
			boolean pipi = cs.execute();
			System.out.println("Resultado booleano: " + pipi);
			return;
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public void Delete(int id) throws Exception{
		try{
			Connection oCnn = Datos.General.CrearConexion();
			String storedProcedure = "{call cpd.ContraparteDelete(?)}";
			CallableStatement cs =oCnn.prepareCall(storedProcedure);
			cs.setInt("mIdContraparte", id);

			boolean pipi = cs.execute();
			System.out.println("Resultado booleano: " + pipi);
			return;
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public List GetOne(int id) throws Exception{
		ResultSet rs = null;	//
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.ContraparteGetOne(?)}";
		CallableStatement cs = oCnn.prepareCall(storedProcedure);
		cs.setInt("mIdContraparte", id);
		rs = cs.executeQuery();
		return sacaDatos(rs);
	}

	public List GetAll() throws Exception{
		ResultSet rs = null;	//
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.ContraparteGetAll()}";
		CallableStatement cs = oCnn.prepareCall(storedProcedure);
		rs = cs.executeQuery();
		return sacaDatos(rs);	
	}
	public List Primero() throws Exception{
		return Trae("FIRST");
	}
	public List Siguiente() throws Exception{
		return Trae("NEXT");
	}
	public List Anterior() throws Exception{
		return Trae("PREV");
	}
	public List Ultimo() throws Exception{
		return Trae("LAST");
	}
	public void Abrir() throws Exception{
		oSesion = new Sesion();
		try{
			String sql = "HANDLER `contraparte` OPEN";
			oSesion.getSesion().executeQuery(sql);
		} catch (Exception ex){
			System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public void Cerrar() throws Exception{
		try{
			String sql = "HANDLER `contraparte` CLOSE";
			oSesion.getSesion().executeQuery(sql);
		} catch (Exception ex){
			System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public List Trae(String condicion) throws Exception{
		ResultSet rs = null;	//
		String sql = "HANDLER `contraparte` READ `PRIMARY` " + condicion + "";
		rs = oSesion.getSesion().executeQuery(sql);  //
		return sacaDatos(rs);
	}
	private void poneDatos(Entidades.Contraparte item, CallableStatement cs){
		try{
			cs.setInt("mIdContraparte", item.getIdContraparte());
			cs.setString("mDscContraparte", item.getDscContraparte());
			cs.setString("mIdPlaza", item.getPlaza().getIdPlaza());
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}
	private List sacaDatos(ResultSet rs){
		List oDatos = new ArrayList();
		Entidades.Contraparte oDato = null;
		try {
			while(rs.next()){
				List oSubDatos = new ArrayList();
				try {
					Entidades.Plaza oSubDato = null;
					oSubDatos = new Datos.Plaza().GetOne(rs.getString(3));
					while(!oSubDatos.isEmpty()){
						oSubDato = (Entidades.Plaza) oSubDatos.remove(0);
					}
					oDato = new Entidades.Contraparte(
							rs.getInt(1),
							rs.getString(2),
							oSubDato
							);
					oDatos.add(oDato);
					oDato = null;
				} catch (Exception ex){
					System.out.println("SQLException: " + ex.getMessage());
				}
			}
			rs.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return oDatos;
	}

}
